package com.hudongwx.my_shian.fragment.communityFragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hudongwx.my_shian.R;
import com.hudongwx.my_shian.utils.API;
import com.hudongwx.my_shian.utils.MyJsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * A simple {@link Fragment} subclass.
 */
public class My_Sun_2_Fragment extends Fragment {
    private ListView listView;
    private View view;
    private JSONArray data;
    private MySunAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null == view){
            view = inflater.inflate(R.layout.fragment_my_sun_2, container, false);
            //获取控件
            listView = (ListView) view.findViewById(R.id.fragment_mySun_list);
            //适配器
            adapter = new MySunAdapter(data);
            //绑定适配器到listView
            listView.setAdapter(adapter);

            updateData();
        }
        return view;
    }

    public void updateData(){
        RequestParams params = new RequestParams(API.getCommunitySun2Url());
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    adapter.updateData(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //适配器
    class MySunAdapter extends BaseAdapter{
        JSONArray data;

        public MySunAdapter(JSONArray data) {
            this.data = data;
        }

        public void updateData(JSONArray data){
            this.data=data;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            if (data==null)return 0;
            return data.length();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;

            if (null==convertView){//没有可以复用的布局
                //解析布局
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_my_sun_2_item,null);
                //实例化一个ViewHolder
                viewHolder = new ViewHolder();

                //找出需要显示的控件
                viewHolder.textView1 = (TextView) convertView.findViewById(R.id.fragment_my_sun_2_item_tv1);
                viewHolder.textView2 = (TextView) convertView.findViewById(R.id.fragment_my_sun_2_item_tv2);
                viewHolder.textView3 = (TextView) convertView.findViewById(R.id.fragment_my_sun_2_item_tv3);
                viewHolder.textView4 = (TextView) convertView.findViewById(R.id.fragment_my_sun_2_item_tv4);

                viewHolder.imageView1 = (ImageView) convertView.findViewById(R.id.fragment_my_sun_2_item_iv1);
                viewHolder.imageView2 = (ImageView) convertView.findViewById(R.id.fragment_my_sun_2_item_iv2);
                viewHolder.imageView3 = (ImageView) convertView.findViewById(R.id.fragment_my_sun_2_item_iv3);
                viewHolder.imageView4 = (ImageView) convertView.findViewById(R.id.fragment_my_sun_2_item_iv4);

                convertView.setTag(viewHolder);
            }else {
                //有可以复用的布局
                viewHolder = (ViewHolder) convertView.getTag();
            }

            JSONArray arr = null;
            try {
                arr = data.getJSONObject(position).getJSONArray("ListFile");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            //把数据设置给控件
            try {
                if (arr.length()==1){
                    x.image().bind(viewHolder.imageView2,data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(0).getString("Url"));
                }else if (arr.length()==2){
                    x.image().bind(viewHolder.imageView2,data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(0).getString("Url"));
                    x.image().bind(viewHolder.imageView3,data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(1).getString("Url"));

                }else if (arr.length()>=3){
                    x.image().bind(viewHolder.imageView2,data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(0).getString("Url"));
                    x.image().bind(viewHolder.imageView3,data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(1).getString("Url"));
                    x.image().bind(viewHolder.imageView4,data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(2).getString("Url"));
                }

                x.image().bind(viewHolder.imageView1,data.getJSONObject(position).getString("UserIcon"));

                viewHolder.textView1.setText(data.getJSONObject(position).getString("UserName"));
                viewHolder.textView2.setText(data.getJSONObject(position).getString("DateTime"));
                viewHolder.textView3.setText(data.getJSONObject(position).getString("Title"));
                viewHolder.textView4.setText(data.getJSONObject(position).getString("Content"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //设置item的高
//            LinearLayout.LayoutParams pa=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,500);
//            convertView.setLayoutParams(pa);
            return convertView;
        }
    }

    class ViewHolder{
        ImageView imageView1;
        ImageView imageView2;
        ImageView imageView3;
        ImageView imageView4;

        TextView textView1;
        TextView textView2;
        TextView textView3;
        TextView textView4;
    }
}
